var fileType = getUrlParam("fileType");
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return ""; //返回参数值
}
var app = angular.module('knowledgeList', []);
// var myScroll = new IScroll('#wrapper', {
//     mouseWheel: true
// });
app.controller('listController', function ($scope,$http) {
    $scope.changeGroupId = fileType;
    getParentGroup();
    // getData();
    $scope.types = [];
    $scope.datas = [];
    $scope.parentCenters = [];
    function getParentGroup(){
        $http.post("listKnowledgeGroup",
            {
                "parentId": 0
            }
        ).then(function (result) {
            console.log(result)
            $scope.types = result.data&& result.data.obj;
            if($scope.types.length >0){
                if($scope.changeGroupId == "")
                    $scope.changeGroupId =  $scope.types[0].id;
                setParentCenters()

            }

        });
    };
    function setParentCenters(){
        for(j in $scope.types){
            if($scope.types[j].id== $scope.changeGroupId){
                $scope.parentCenters.splice(0,$scope.parentCenters.length);
                for(i in $scope.types[j].centers){
                    $scope.parentCenters.push($scope.types[j].centers[i]);
                }
            }
        }

    }
    $scope.$watch("changeGroupId", function(n, o){
        setParentCenters();
        getGroup();
    })
    function getGroup(){
        if(!$scope.changeGroupId)return;
        $http.post("listKnowledgeGroup",
            {
                "parentId":  $scope.changeGroupId
            }
        ).then(function (result) {
            console.log(result)
            $scope.groupList = result.data&& result.data.obj;
        });
    };
    function getData() {

        $http.get("getList",{
            params: {
                "fileType": $scope.changeType
            }
        }).then(function (result) {
            $scope.datas = result.data&& result.data.list;

        });
    };
    $scope.toDownLoad = function(url){
      //  $(event.target).attr("href",url);
        location.href = url;
      //  $(event.target).children("span").click();
    }
    $scope.toDetail = function(rowId) {
         location.href="detail/"+rowId+"?fileType="+$scope.changeGroupId;
    };
    $scope.formateDate = function (date){
        return new Date(date).format("yyyy-MM-dd")
    }
    $scope.typeChange = function (pid){
        $scope.changeGroupId = pid;
        // getGroup();
    }
});

